<?php
/**
 *@作者:MissZhang
 *@邮箱:<787727147@qq.com>
 *@创建时间:2021/9/30 上午10:59
 *@说明:商家信息控制器
 */

namespace app\store\controller;

use app\common\model\AccountLog;
use app\common\model\StoreCollect;
use app\common\model\StoreLog;
use app\common\model\ThirdUser;
use app\common\model\Withdrawals;
use app\common\util\AdminException;
use think\exception\ValidateException;
use think\facade\Db;
use think\facade\View;

class Store extends Base
{
    //店铺设置
    public function setting(){
        if (IS_POST){
            $data=input('post.');
            try {
                validate(\app\store\validate\Store::class)->batch(true)->check($data);
            }catch (ValidateException $e){
                $error=$e->getError();
                $values=array_values($error);
                $result['result']=$error;
                throw new AdminException($values[0],10,$result);
            }
            $store_model=new \app\common\model\Store();
            $info=$store_model->find($this->dianpu_id);
            $url=url('Store/setting')->build();
            $data['store_person_cert']=str_replace(SITE_URL,'',$data['store_person_cert']);
            $data['store_person_cert1']=str_replace(SITE_URL,'',$data['store_person_cert1']);
            $data['store_logo']=str_replace(SITE_URL,'',$data['store_logo']);
            $data['business_licence_cert']=str_replace(SITE_URL,'',$data['business_licence_cert']);
            $res=$info->save($data);
            if ($res){
                storeLog("修改商家信息");
                throw new AdminException('操作成功',1,['url'=>$url]);
            }else{
                throw new AdminException('操作失败');
            }
        }
        return view();
    }
    //申请提现
    public function withdraw(){
        $cash=getSysConfig('cash');
        //判断是否绑定了微信和支付宝 至少绑定一个
        $is_wx_bind=$is_zfb_bind=0;
        //判断是否绑定了微信
        $user=$this->store['user'];
        $third_find=ThirdUser::where("uid=$this->user_id and type=2")->find();
        if ($third_find){
            $is_wx_bind=1;
        }
        //判断是否绑定了支付宝
        if ($user['zhifubao'] && $user['realname']){
            $is_zfb_bind=1;
        }
        if ($is_wx_bind==0 && $is_zfb_bind==0){
            $this->error("请在APP端完善提现账户后在进行提现");
        }
        if ($is_wx_bind==1 && $is_zfb_bind==1){
            $temp_tx_type=1;
        }elseif ($is_wx_bind){
            $temp_tx_type=1;
        }else{
            $temp_tx_type=2;
        }
        if (IS_POST){
            if ($cash['cash_open']==0){
                throw new AdminException('提现暂未开放');
            }
            $money = input('money');
            $tx_type = input('tx_type');
            if ($money=='' || !is_numeric($money) || $money<=0){
                throw new AdminException('请输入正确的提现金额');
            }
            if ($tx_type==1 && $is_wx_bind==0){
                throw new AdminException('非法提交');
            }
            if ($tx_type==2 && $is_zfb_bind==0){
                throw new AdminException('非法提交');
            }
            $store_tx_sxf         =    $cash['ye_tx_sxf'];//粮票提现手续费
            $store_tx_min         =    $cash['ye_tx_min'];//粮票最少提现
            $store_tx_xishu       =    $cash['ye_tx_xishu'];//粮票提现系数
            $store_tx_num         =    $cash['ye_tx_num'];//粮票每日可提现次数
            if ($store_tx_num>0){
                $count_where[]=['user_id','=',$this->user_id];
                $count_where[]=['type','=',1];
                $count=Db::name('withdrawals')
                    ->where($count_where)
                    ->whereDay('create_time')
                    ->count();
                if ($count>=$store_tx_num){
                    throw new AdminException("每日可提现{$store_tx_num}次");
                }
            }
            if ($money>$user['user_money']){
                throw new AdminException("粮票不足,可用{$user['user_money']}元!");
            }
            if ($store_tx_xishu>0) {
                if (($money*100000)%($store_tx_xishu*100000)!=0){
                    throw new AdminException("提现金额为{$store_tx_xishu}元的整数倍!");
                }
            }
            if ($store_tx_min>0) {
                if ($money<$store_tx_min){
                    throw new AdminException("最少提现金额{$store_tx_min}元起");
                }
            }
            $taxfee=$money*($store_tx_sxf/100);
            $insert['order_sn'] = getOrderSn('withdrawals','w');
            $insert['user_id'] = $this->user_id;
            $insert['tx_type'] = $tx_type;
            $insert['money'] = $money-$taxfee;
            $insert['taxfee'] = $taxfee;
            $insert['true_money'] = $money - $insert['taxfee'];
            $insert['zhifubao'] = $user['zhifubao'];
            $insert['realname'] = $user['realname'];
            $res=Withdrawals::create($insert);
            $log=accountLog($this->user_id,-$money,'申请提现');
            if ($res && $log) {
                throw new AdminException('已提交申请',1,['url'=>url('Store/withdraw_list')->build()]);
            } else {
                throw new AdminException('提交失败');
            }
        }
        View::assign('cash', $cash);
        View::assign('is_wx_bind', $is_wx_bind);
        View::assign('is_zfb_bind', $is_zfb_bind);
        View::assign('temp_tx_type', $temp_tx_type);
        return view();
    }
    //提现记录
    public function withdraw_list(){
        // 搜索条件
        $where[] = ['user_id','=',$this->user_id];
        $status = input('status');
        $tx_type = input('tx_type');
        if ($status!=''){
            $where[]=['status','=',$status];
        }
        if ($tx_type){
            $where[]=['tx_type','=',$tx_type];
        }
        $list = Withdrawals::where($where)->whereBetween('create_time',"$this->begin,$this->end")
            ->order('id desc')
            ->paginate([
                'query'=>['status'=>$status,'tx_type'=>$tx_type],
                'list_rows'=>$this->page_size
            ]);
        // 获取分页显示
        $page = $list->render();
        View::assign('list', $list);
        View::assign('page', $page);
        return view();
    }
    //导出提现
    public function export_with(){
        //搜索条件
        $ids = input('ids');
        // 搜索条件
        $where[] = ['user_id','=',$this->user_id];
        if($ids){
            $where[] = ['id','in', $ids];
        }else{
            $status = input('status');
            $tx_type = input('tx_type');
            if ($status!=''){
                $where[]=['status','=',$status];
            }
            if ($tx_type){
                $where[]=['tx_type','=',$tx_type];
            }
        }
        $userList = Withdrawals::where($where)
            ->whereBetween('create_time',"$this->begin,$this->end")
            ->order('id desc')
            ->select();
        $strTable ='<table width="500" border="1">';
        $strTable .= '<tr>';
        $strTable .= '<td style="text-align:center;font-size:12px;">编号</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;">申请金额</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;">手续费</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;">实际到账</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;">提现方式</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;">提现信息</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;">状态</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;">申请时间</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;">审核时间</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;">拒绝时间</td>';
        $strTable .= '</tr>';
        if($userList){
            foreach($userList as $k=>$val){
                $strTable .= '<tr>';
                $strTable .= '<td style="text-align:center;font-size:12px;">&nbsp;'.$val['id'].'</td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">'.$val['money'].'</td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">'.$val['taxfee'].'</td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">'.$val['true_money'].'</td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">'.$val['tx_type_text'].'</td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">'.$val['zhanghu'].'</td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">'.$val['status_text'].'</td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">'.$val['create_time'].'</td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">'.$val['check_time'].'</td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">'.$val['refuse_time'].'</td>';
                $strTable .= '</tr>';
            }
        }
        $strTable .='</table>';
        unset($orderList);
        storeLog("导出提现");
        downloadExcel($strTable,'提现');
        exit();
    }
    //店铺关注列表
    public function store_collect(){
        // 搜索条件
        $where[] = ['store_id','=',$this->dianpu_id];
        $keyword = input('keyword');
        if ($keyword!=''){
            $ids=Db::name('users')->whereLike('mobile',"%$keyword%")->column('user_id');
            $where[]=['user_id','in',$ids];
        }
        $list = StoreCollect::where($where)
            ->order('id desc')
            ->whereBetween('add_time',"$this->begin,$this->end")->paginate([
                'query'=>['keyword'=>$keyword],
                'list_rows'=>$this->page_size
            ]);
        // 获取分页显示
        $page = $list->render();
        View::assign('list', $list);
        View::assign('page', $page);
        return view();
    }
    //会员资金记录
    public function account_log(){
        // 搜索条件
        $where[] = ['user_id','=',$this->user_id];
        $type = input('type');
        $search_key = input('search_key');
        if ($type!=''){
            $where[]=['type','=',$type];
        }
        if ($search_key!=''){
            $where[]=['desc','like',"%$search_key%"];
        }
        $list = AccountLog::where($where)
            ->order('log_id desc')
            ->whereBetween('add_time',"$this->begin,$this->end")->paginate([
                'query'=>['type'=>$type,'search_key'=>$search_key],
                'list_rows'=>$this->page_size
            ]);
        // 获取分页显示
        $page = $list->render();
        $text="我的资金";
        //清除缓存 不然text变量不生效
        $runtimepath=runtime_path();
        delDir($runtimepath."temp/");
        View::assign('list', $list);
        View::assign('page', $page);
        View::assign('text', $text);
        return view();
    }
    //导出资金记录
    public function export_account_log(){
        //搜索条件
        $ids = input('ids');
        // 搜索条件
        $where[] = ['user_id','=',$this->user_id];
        if($ids){
            $where[] = ['log_id','in', $ids];
        }else{
            $type = input('type');
            $search_key = input('search_key');
            if ($type!=''){
                $where[]=['type','=',$type];
            }
            if ($search_key!=''){
                $where[]=['desc','like',"%$search_key%"];
            }
        }
        $orderList = AccountLog::where($where)
            ->order('log_id desc')
            ->whereBetween('add_time',"$this->begin,$this->end")
            ->select();
        $strTable ='<table width="500" border="1">';
        $strTable .= '<tr>';
        $strTable .= '<td style="text-align:center;font-size:12px;width:150px;">编号</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;">贡献者</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;">变动资金</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;">钱包类型</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;width:150px;">订单编号</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;" width="*">说明</td>';
        $strTable .= '<td style="text-align:center;font-size:12px;" width="*">变动时间</td>';
        $strTable .= '</tr>';
        if($orderList){
            foreach($orderList as $k=>$val){
                $order_sn=$val['order_sn'] ?: "无";
                $guser_mobile=$val['gid'] ?  $val['guser']['mobile'] : "---";
                $strTable .= '<tr>';
                $strTable .= '<td style="text-align:center;font-size:12px;">&nbsp;'.$val['log_id'].'</td>';
                $strTable .= '<td style="text-align:center;font-size:12px;">'.$guser_mobile.'</td>';
                $strTable .= '<td style="text-align:center;font-size:12px;">'.$val['money'].'</td>';
                $strTable .= '<td style="text-align:center;font-size:12px;">'.$val['type_text'].'</td>';
                $strTable .= '<td style="text-align:center;font-size:12px;">&nbsp;'.$order_sn.'</td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">'.$val['desc'].'</td>';
                $strTable .= '<td style="text-align:left;font-size:12px;">'.$val['add_time'].'</td>';
                $strTable .= '</tr>';
            }
        }
        $strTable .='</table>';
        unset($orderList);
        storeLog("导出我的资金");
        downloadExcel($strTable,'我的资金');
        exit();
    }
    //管理员日志
    public function store_log(){
        // 搜索条件
        $where[] = ['store_id','=',$this->dianpu_id];
        $keyword = input('keyword');
        if ($keyword){
            $where[]=['log_info','like',"%$keyword%"];
        }
        $list = StoreLog::where($where)->order('id desc')->paginate([
            'query'=>['keyword'=>$keyword],
            'list_rows'=>$this->page_size
        ]);
        // 获取分页显示
        $page = $list->render();
        View::assign('list', $list);
        View::assign('page', $page);
        return view();
    }
}
